<?php
namespace Admin\Controller;
use Think\Controller;
class IndexController extends Controller {

    public function index(){
        layout(false);
      	$this->display();
    }

    public function login(){
        $data = I('post.');
        $name = $data['name'];
        session('admin',$name);  //设置session
        $pwd  = md5($data['password']);
        $map['name&pwd'] =array($name,$pwd,'_multi'=>true);
        $admin = M('admin');
        $admin->where($map)->find();
        if(M('admin')->where($map)->find()){
            echo "<script> window.location.href='./main';</script>";
        }
        //$this->redirect('index/main', array('id' => $id), 0, '修改成功');alert('登录成功！')
        else{
          // $this->error("用户名或者密码错误！",$this->site_url,1);  
        }
    }

/*    public function verify(){  
        $Verify = new \Think\Verify();  
        $Verify->fontSize = 18;  
        $Verify->length   = 4;  
        $Verify->useNoise = false;  
        $Verify->codeSet = '0123456789';  
        $Verify->imageW = 130;  
        $Verify->imageH = 50;  
        //$Verify->expire = 600;  
        $Verify->entry(); 
}*/ 
    public function order(){  
        $openid=1;
        $order=M('order');
        $data=M('order')->select();
        $i=0;
        $user=M('consumer');
        foreach ($data as $key => $value) {
            $da=$value['user'];
            $re=$user->field('wxname')->select();
            $data[$i]['state']="";
            $i++;
        }
        $this->assign('data',$data);
        $this->display();
    }

    public function admin_role(){

       $this->display();
    }

    public function viplist(){
        $db = M("consumer");
        $map['status']  = array('egt',4);
        //$count = count($db->where($map)->select());
        //print_r($count);exit();
        $count      = $db->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,5);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出
       /* $pagecount = 10;
        $page = new \Think\Page($count , $pagecount);
        $page->parameter = $row; //此处的row是数组，为了传递查询条件
        $page->setConfig('first','首页');
        $page->setConfig('prev','上一页');
        $page->setConfig('next','下一页');
        $page->setConfig('last','尾页');
        $page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% 第 '.I('p',1).' 页/共 %TOTAL_PAGE% 页 ( '.$pagecount.' 条/页 共 %TOTAL_ROW% 条)');*/
        //$show = $page->show();
        //$list = $db->where($map)->limit($page->firstRow.','.$page->listRows)->select();
        //print_r($list);exit();
        $list = $db->order('id')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('data',$list);
        $this->assign('page',$show);
        $this->display();
/*
        //$User = M('User'); // 实例化User对象
        $count      = $User->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,5);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $User->where('status=1')->order('create_time')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->display(); // 输出模板
*/
    }
    public function goodslist(){  
        $db = M("goods");
        $map['status']  = array('egt',4);
        $count = count($db->where($map)->select());
        //print_r($count);exit();
        $pagecount = 10;
        $page = new \Think\Page($count , $pagecount);
        $page->parameter = $row; //此处的row是数组，为了传递查询条件
        $page->setConfig('first','首页');
        $page->setConfig('prev','上一页');
        $page->setConfig('next','下一页');
        $page->setConfig('last','尾页');
        $page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% 第 '.I('p',1).' 页/共 %TOTAL_PAGE% 页 ( '.$pagecount.' 条/页 共 %TOTAL_ROW% 条)');
        $show = $page->show();
        $list = $db->where($map)->limit($page->firstRow.','.$page->listRows)->select();
        $this->assign('data',$list);
        $this->assign('page',$show);
        $this->display();
    }

    public function about_us(){
        $db=M('about_us');
        $data=$db->where('id=1')->find();
        //print_r($data);exit();
        $this->assign('data',$data);
        $this->display();
    }

    public function coupon(){
        $db = M("coupon");
        $map['status']  = array('egt',4);
        $count = count($db->where($map)->select());
        //print_r($count);exit();
        $pagecount = 10;
        $page = new \Think\Page($count , $pagecount);
        $page->parameter = $row; //此处的row是数组，为了传递查询条件
        $page->setConfig('first','首页');
        $page->setConfig('prev','上一页');
        $page->setConfig('next','下一页');
        $page->setConfig('last','尾页');
        $page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% 第 '.I('p',1).' 页/共 %TOTAL_PAGE% 页 ( '.$pagecount.' 条/页 共 %TOTAL_ROW% 条)');
        $show = $page->show();
        $list = $db->where($map)->limit($page->firstRow.','.$page->listRows)->select();
        $this->assign('data',$list);
        $this->assign('page',$show);
        $this->display();
    }
    public function adminlist(){ 
        $name=session('admin');
        $re=M('admin')->where("name=$name")->find();
        $user=M('admin');
        $power= $re['power'];
        if ($power==1) {
            echo "<script> alert('只有超级管理员能查看！');window.location.href='./main';</script>";
        }
        $data = $user->select();
        $i=0;
        foreach ($data as $key => $value) {
            if($value['power']==0){
                $data[$i]['permission']="";
            }
            else{
                $data[$i]['permission']="selected";
            }
            $i++;
        }
        $this->assign('data',$data);
        $this->display();   
    }

    public function vipdetails() {
        $id=I('get.id');
        $information = M('consumer')->where("id='$id'")->find();
        $openid = $information['openid'];
        $coupon_data = M('user_coupon')->where("user_openid = '".$openid."'")->select();
        $address = M('address')->where('openid ="'.$openid.'"')->select();
        var_dump($coupon_data);
        var_dump($address);die;
        $this->assign('coupon_data',$coupon_data);
        $this->assign('address',$address);
        $this->assign('re',$re);
        $this->display();
    }

    public function  search(){
        $word=I('post.word');
        $word="%".$word."%";
        //print_r($word);exit();
        $map['wxname']=array('like',$word);
        $db=M('consumer');
    
        $count = count($db->where($map)->select());
        //print_r($count);exit();
        $pagecount = 5;
        $page = new \Think\Page($count , $pagecount);
        $page->parameter = $row; //此处的row是数组，为了传递查询条件
        $page->setConfig('first','首页');
        $page->setConfig('prev','上一页');
        $page->setConfig('next','下一页');
        $page->setConfig('last','尾页');
        $page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% 第 '.I('p',1).' 页/共 %TOTAL_PAGE% 页 ( '.$pagecount.' 条/页 共 %TOTAL_ROW% 条)');
        $show = $page->show();
        $list = $db->where($map)->order('id desc')->limit($page->firstRow.','.$page->listRows)->select();
        //print_r($list);exit();
        $this->assign('data',$list);
        $this->assign('page',$show);
        $this->display();
    }

    public function ico(){   

        $db=M('banner');                       
        $data=$db->select();   
        //$map['id']  = array('egt',1);
        $count = count($db->select());
        //print_r($count);exit();
        $pagecount = 5;
        $page = new \Think\Page($count , $pagecount);
        $page->parameter = $row; //此处的row是数组，为了传递查询条件
        $page->setConfig('first','首页');
        $page->setConfig('prev','上一页');
        $page->setConfig('next','下一页');
        $page->setConfig('last','尾页');
        $page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% 第 '.I('p',1).' 页/共 %TOTAL_PAGE% 页 ( '.$pagecount.' 条/页 共 %TOTAL_ROW% 条)');
        $show = $page->show();
        $list = $db->order('id desc')->limit($page->firstRow.','.$page->listRows)->select();
        //print_r($list);exit();
        $this->assign('data',$data);
        $this->assign('page',$show);
        $this->display();
    }

    public function add_banner(){
        $this->display();
    }

    public function upload()
    {

        //$data['name']=I('post.name');
        //$db=M('banner');
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     314572800000 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =     './Uploads/'; // 设置附件上传根目录
        $upload->savePath  =     ''; // 设置附件上传（子）目录
        $upload->saveName = 'time';     //命名方式
        //$data = $info['photo']['name'];

        $info   =   $upload->upload();
        $img_name=$info ['photo']['savepath'].$info ['photo']['savename'];
        return $img_name;
    /*    if ($data['name']=='') {
            $this->error("banner名字不能为空！",$this->site_url,1);  
        }
        if(!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        }else{// 上传成功
           // print_r($info);exit();
            $data['b_url']=$info ['photo']['savepath'].$info ['photo']['savename'];

            //print_r($data);exit();
            $db->data($data)->add();
            $this->redirect('index/ico', array('id' => $id), 1, '上传成功');
        }*/
    }

    public function delbanner($value=''){
        $id=I('get.');
        //print_r($id);exit();
        $id=$id['id'];
        $banner=M('banner');
        $result= $banner->where("id=$id")->delete();  
       // print_r($result); 
        if ( false !== $result ){   
        $this->success('删除成功！');   
        }else{   
        $this->error('删除失败！');   
        }
    }

    public function re_banner($value=''){
        $data=I('POST.');
        $dba=M('about_us');
        //print_r($data);
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     314572800000 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =     './Uploads/'; // 设置附件上传根目录
        $upload->savePath  =     ''; // 设置附件上传（子）目录
        $upload->saveName = 'time';     //命名方式
        //$data = $info['photo']['name'];
        $info   =   $upload->upload();

        $data['id']=1;
         if ($info=='') {
            $dba->save($data);  
        } else {
            $data['tupian']=$info ['photo']['savepath'].$info ['photo']['savename'];
            $dba->where("id=1")->save($data);
        }
        if ($dba=='') {
        $this->error('删除失败！'); 
        } else {
            $this->success('修改成功！'); 
        }
    }


    public function details_banner(){
        $id=I('get.id');
        //print_r($id);
        $banner=M('banner');
        $data=$banner->where("id=$id")->find();
        //print_r($data);
        $this->assign('data',$data);
        //$this->assign('data',$data);
        $this->display();
    }

    public function xg_banner($value=''){
        $data['name']=I('post.name');
        $id=I('post.id');
        $img_name=$this->upload();
        
        $db=M('banner');
        if ($img_name=='') {
            $db->where("id='$id'")->save($data);  
        } else {
            $data['imgurl']=$img_name;
            $db->where("id='$id'")->save($data);
        }
        if ($db=='') {
        $this->error('删除失败！'); 
        } else {
            $this->redirect('ico');
        }
    }


    public function goodstails(){
        $id=I('get.id');
        $db=M('goods');
        $data=$db->where("id=$id")->find();
       // $pay=M('goodstime');
        //$gid=$data['id'];
        //$da=$pay->where("gid=$gid")->select();
        //print_r($da);exit();
        $this->assign('da',$da);
        $this->assign('data',$data);
        $this->display();
    }

    public function add_coupon($value=''){
        $data=I('post.');
        $img_name=$this->upload();
        $data['imgurl']=$img_name;
        $re=M('banner')->data($data)->add();
            
        //print_r($data);exit();
        if ($data='') {
            $this->display();
        } else {
            if ($re=='') {
                $this->error('上传失败！');  
            } else {
                $this->success('新增成功', './ico');
            }  
        }
        
        
    }

    public function del(){
        $key=I('get.key');
        $id=I('get.id');
        $db=M($key);
        //print_r($db);exit();
        $result= $db->where("id=$id")->delete();  
       // print_r($result); 
        if ( false !== $result ){   
            $this->redirect('index/goodslist');
        }else{   
        $this->error('删除失败！');   
        }
    }

    public function re_goods(){
        $data=I('post.');
        $M = M('goods')->where('id='.$data["id"])->save($data);
        $this->redirect('index/goodslist');
    }




























}

?>
